<template>
  <a-modal
    :visible="visible"
    title="修改实训室"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
      ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item ref="lainName" label="实训室名称" name="lainName">
        <a-input v-model:value="formState.lainName"/>
      </a-form-item>
      <a-form-item ref="propleNumber" label="限制人数" name="propleNumber">
        <a-input type="number" v-model:value="formState.propleNumber"/>
      </a-form-item>
      <a-form-item ref="lainLocation" label="所处位置" name="lainLocation">
        <a-input v-model:value="formState.lainLocation"/>
      </a-form-item>
      <a-form-item ref="content" label="备注" name="remark">
        <a-textarea v-model:value="formState.remark"/>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import {message} from 'ant-design-vue';
import {defineComponent, reactive, ref, toRaw, watch} from "vue";
import {save, Update} from "@/api/module/laboratoryInfo";

export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object,
    }
  },
  emit: ["close"],
  setup(props, context) {

    const formRef = ref();

    const formState = reactive({});

    const formRules = {};

    const saveKey = "save";

    watch(props, (props) => {
      formState.id = props.record.id
      formState.lainName = props.record.lainName
      formState.lainState = props.record.title
      formState.propleNumber = props.record.propleNumber
      formState.lainLocation = props.record.lainLocation
      formState.remark = props.record.remark
    })

    const submit = (e) => {
      message.loading({content: '提交中...', key: saveKey});
      formRef.value
        .validate()
        .then(() => {
          Update(toRaw(formState)).then((response) => {
            if (response.success) {
              message.success({content: '保存成功', key: saveKey, duration: 1}).then(() => {
                cancel();
              });
            } else {
              message.success({content: '保存失败', key: saveKey, duration: 1}).then(() => {
                cancel();
              });
            }
          });
        })
        .catch(error => {
          console.log('error', error);
        });
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    return {
      submit,
      cancel,
      formRef,
      formState,
      formRules,

      labelCol: {span: 6},
      wrapperCol: {span: 18},
    };
  },
});
</script>
